package com.sgb.kjwl.view.ui.adapter.machine;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.sgb.kjwl.BR;
import com.sgb.kjwl.R;
import com.sgb.kjwl.api.APIManager;
import com.sgb.kjwl.model.entity.lease.MyAskLeaseQutedEntity;
import com.swgk.core.dialog.RoundCornerDialog;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.recyclerview.adapter.BaseRecycleAdapter;
import com.swgk.core.recyclerview.adapter.ItemViewHolder;
import com.swgk.core.util.DateUtils;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class MyAskLeaseQutedAdapter extends BaseRecycleAdapter<MyAskLeaseQutedEntity> {

    public MyAskLeaseQutedAdapter(Context context, List<MyAskLeaseQutedEntity> datas) {
        super(context, R.layout.item_my_ask_lease_quted, datas);
    }

    /**
     * @param holder
     * @param goodsInfoEntity3
     */
    @Override
    public void convert(ItemViewHolder holder, MyAskLeaseQutedEntity goodsInfoEntity3) {
        holder.setBinding(BR.viewModel, goodsInfoEntity3);

        LinearLayout llBidder = holder.getItem().findViewById(R.id.ll_bidder);
        TextView tvBidder = holder.getItem().findViewById(R.id.tv_bidder);
        TextView tvAccept = holder.getItem().findViewById(R.id.view_accept);
        TextView tvIgnore = holder.getItem().findViewById(R.id.view_ignore);
        TextView phone = holder.getItem().findViewById(R.id.phone);
        TextView tvLeaseTime = holder.getItem().findViewById(R.id.tv_lease_time);
        RelativeLayout acceptLayout = holder.getItem().findViewById(R.id.acceptLayout);
        TextView acceptStatus = holder.getItem().findViewById(R.id.acceptStatus);
        TextView tvRemark = holder.getItem().findViewById(R.id.tv_remark);

        if (TextUtils.isEmpty(goodsInfoEntity3.getRemark())) {
            tvRemark.setText("暂无信息");
        } else {
            tvRemark.setText(goodsInfoEntity3.getRemark());
        }

        //默认
        acceptLayout.setVisibility(View.GONE);
        acceptStatus.setVisibility(View.VISIBLE);

        //0报价中，1接受，2，已忽略,3 已失效
        if (goodsInfoEntity3.getIsAcceptQuote() == 0) {
            acceptLayout.setVisibility(View.VISIBLE);
            acceptStatus.setVisibility(View.GONE);
        } else if (goodsInfoEntity3.getIsAcceptQuote() == 1) {
            acceptStatus.setText("您已接受该条报价");
        } else if (goodsInfoEntity3.getIsAcceptQuote() == 2) {
            acceptStatus.setText("您已忽略该条报价");
        } else if (goodsInfoEntity3.getIsAcceptQuote() == 3) {
            acceptStatus.setText("该条报价已失效");
        }
        //已完成状态下,隐藏操作按钮
        if (goodsInfoEntity3.getStatus() == 4) {
            acceptLayout.setVisibility(View.GONE);
            acceptStatus.setVisibility(View.VISIBLE);
        }

        phone.setOnClickListener(v -> showDialog(goodsInfoEntity3.getCreatePhone()));

        tvAccept.setOnClickListener(v -> {
            acceptPrice(goodsInfoEntity3.getId(), goodsInfoEntity3.getRentalSeekId(), goodsInfoEntity3.getCategoryType());
        });

        tvIgnore.setOnClickListener(v -> {
            ignorePrice(goodsInfoEntity3.getId(), goodsInfoEntity3.getUpdateTime(), goodsInfoEntity3.getCategoryType());
        });

        if (TextUtils.isEmpty(goodsInfoEntity3.getCreateCompanyName())) {
            llBidder.setVisibility(View.GONE);
        } else {
            llBidder.setVisibility(View.VISIBLE);
            tvBidder.setText(goodsInfoEntity3.getCreateCompanyName());
        }

        try {
            tvLeaseTime.setText(DateUtils.stampToDate(goodsInfoEntity3.getCreateTime()));
        } catch (Exception e) {
        }

    }

    //接受报价
    private void acceptPrice(String id, String no, String categoryType) {
        DialogHelper.showProgressDialog(context, "提交中", 0, false);
        APIManager.getInstance().getMachineAPI().acceptOffer(no, id, categoryType).enqueue(new Callback<BaseEntity>() {
            @Override
            public void onResponse(Call<BaseEntity> call, Response<BaseEntity> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }

                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    if (onmyaskleasequted != null) {
                        onmyaskleasequted.getDataList();
                    }
                }
            }

            @Override
            public void onFailure(Call<BaseEntity> call, Throwable t) {
                Log.v("dd", "sdsdd");
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    //忽略报价
    private void ignorePrice(String id, String time, String categoryType) {
        DialogHelper.showProgressDialog(context, "提交中", 0, false);
        APIManager.getInstance().getMachineAPI().ignoreMatch(id, time, categoryType).enqueue(new Callback<BaseEntity>() {
            @Override
            public void onResponse(Call<BaseEntity> call, Response<BaseEntity> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    if (onmyaskleasequted != null) {
                        onmyaskleasequted.getDataList();
                    }
                }
            }

            @Override
            public void onFailure(Call<BaseEntity> call, Throwable t) {
                Log.v("dd", "sdsdd");
                DialogHelper.dismissProgressDialog();
            }
        });
    }


    //打电话
    public void showDialog(String phone) {
        View view = View.inflate(context, R.layout.dialog_two_btn, null);
        final RoundCornerDialog roundCornerDialog = new RoundCornerDialog(context, 0, 0, view, R.style.RoundCornerDialog);
        roundCornerDialog.show();
        roundCornerDialog.setCanceledOnTouchOutside(false);// 设置点击屏幕Dialog不消失

        TextView tv_message = (TextView) view.findViewById(R.id.tv_message);
        TextView tv_title = view.findViewById(R.id.tv_title);
        TextView tv_logout_confirm = (TextView) view.findViewById(R.id.tv_logout_confirm);
        tv_title.setVisibility(View.VISIBLE);
        TextView tv_logout_cancel = (TextView) view.findViewById(R.id.tv_logout_cancel);
        tv_title.setText("确认呼叫");
        tv_message.setText(phone);

        //确定
        tv_logout_confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent intent = new Intent(Intent.ACTION_DIAL);
                Uri data = Uri.parse("tel:" + phone);
                intent.setData(data);
                context.startActivity(intent);
                roundCornerDialog.dismiss();
            }
        });

        //取消
        tv_logout_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                roundCornerDialog.dismiss();
            }
        });
    }

    private OnMyAskLeaseQuted onmyaskleasequted;

    public interface OnMyAskLeaseQuted {
        void getDataList();

    }

    public void setOnMyAskLeaseQuted(OnMyAskLeaseQuted onmyaskleasequted) {
        this.onmyaskleasequted = onmyaskleasequted;
    }
}
